/*
 * Copyright (c) 2019. Dxxld@qq.com. All rights reserved.
 */

package com.wauil.keenote.algorithm;

/**
 * @Description: 冒泡排序算法实现，一次把最大的值冒泡至最后
 * @Author: kee
 * @Email: dxxld@qq.com
 * @Date: 2019/12/20 10:47
 * @Version: 1.0
 */
public class BubbleSort {
    /**
     * 普通实现 常规实现
     */
    public void sort(int[] array) {
        //最外层循环，从第一个元素开始
        for (int i = 0; i < array.length - 1; i++) {
            //内层循环
            boolean hasChanged = false;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    hasChanged = true;
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
            if (!hasChanged) {
                break;
            }
        }
    }
}
